## Replication of Figure A1 of "Leading Toward Equality: The Effect of Women Mayors 
## on Gender Equality in Local Bureaucracies." 2017. 
## Published at Politics, Groups, and Identities.
## Authors: Kendall Funk, Thiago Silva, and Maria Escobar-Lemmon.
## Date: November, 2017.

## Load in packages
library(readstata13) # package to read Stata file
library(ggplot2) # package to produce the figures
#install.packages("tidyverse") # remove the hashtag in front of the code to install the package tidyverse 
library(tidyverse) # package to create the new variables (mean values per year)
library(gridExtra) # graph combine package

## Setting the directory
setwd("~/Downloads/Replication Files") 

## Opening dataset 
fse <- read.dta13("FSE_Master.dta")

## Replication of Figure A1. Time series for the dependent and main independent variables.

# Plot Percentage of Women Mayors over time (2001-2012)
fse_women_mayors <- fse %>% 
  group_by(year) %>% 
  mutate(women_mayors = (length(femalemayor_new[femalemayor_new==1]) / length(femalemayor_new))*100) %>% 
  distinct(year, .keep_all = TRUE) 

womenmayors = ggplot(fse_women_mayors, aes(year, women_mayors)) + geom_line() + scale_y_continuous(limits = c(6, 10)) +
  scale_x_continuous(breaks = 2001:2012) + xlab("") + ylab("% Women Mayors") + theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 


# Plot Percentage of Women Bureaucrats over time (2001-2012)
fse_women_bureac <- fse %>% 
  group_by(year) %>% 
  mutate(women_bureac = mean(na.omit(pctfempubadmin))) %>% 
  distinct(year, .keep_all = TRUE) 

womenbureac = ggplot(fse_women_bureac, aes(year, women_bureac)) + geom_line() + scale_y_continuous(limits = c(61, 63)) +
  scale_x_continuous(breaks = 2001:2012) + xlab("") + ylab("% Women Bureaucrats") + theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

# Plot Average Women Salary over time (2001-2012)
fse_avg_wom_sal <- fse %>% 
  group_by(year) %>% 
  mutate(avg_fem_sal = mean(na.omit(avgfemsalnommpa))) %>% 
  distinct(year, .keep_all = TRUE) 

avgwomsal = ggplot(fse_avg_wom_sal, aes(year, avg_fem_sal)) + geom_line() + scale_y_continuous(limits = c(350, 1400)) +
  scale_x_continuous(breaks = 2001:2012) + xlab("") + ylab("Avg. Women Salary") + theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

# Plot Average Wage Gap over time (2001-2012)
fse_wage_gap <- fse %>% 
  group_by(year) %>% 
  mutate(wage_gap = mean(na.omit(wagegapsalmpa))) %>% 
  distinct(year, .keep_all = TRUE) 

wagegap = ggplot(fse_wage_gap, aes(year, wage_gap)) + geom_line() + scale_y_continuous(limits = c(50, 200)) +
  scale_x_continuous(breaks = 2001:2012) + xlab("") + ylab("Avg. Wage Gap") + theme_bw() + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

## Graph export in .pdf
pdf("Figure_A1.pdf",
    width=9,height=9) 
grid.arrange(womenmayors, womenbureac, avgwomsal, wagegap, ncol=1) #to divide by column (side-by-side) add ", ncol=2" within the parentheses
dev.off()
